package org.yty.travelagencyredis.Dao.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.yty.travelagencyredis.Dao.GuidebookDao;
import org.yty.travelagencyredis.model.Entity.Guidebook;

@Repository
@RequiredArgsConstructor
public class GuidebookDaoImpl implements GuidebookDao {
    private final StringRedisTemplate redisTemplate;

    @Override
    public Guidebook selectById(Long id) {
        String guidebookJson = (String) this.redisTemplate.opsForHash().get(Guidebook.key, id);
        return JSON.parseObject(guidebookJson, Guidebook.class);
    }

    @Override
    public boolean insert(Guidebook guidebook) {
        this.redisTemplate.opsForHash().put(Guidebook.key, guidebook.getId(), JSON.toJSONString(guidebook));
        return true;
    }

    @Override
    public boolean update(Guidebook guidebook) {
        this.redisTemplate.opsForHash().put(Guidebook.key, guidebook.getId(), JSON.toJSONString(guidebook));
        return true;
    }

    @Override
    public void deleteById(Long id) {
        this.redisTemplate.opsForHash().delete(Guidebook.key, id);
    }
}
